/**
 * 
 * @authors Your Name (you@example.org)
 * @date    2016-10-27 15:02:55
 * @version $Id$
 *
 * 加载订单模块
 *    下载订单数据
 *    渲染页面
 *    开启滑动事件
 *
 */

;(function(root,factory){

  if(typeof exports === 'object' && typeof module === 'object'){
    
    factory(exports);

  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['exports'], factory);
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJS
    factory(exports);
  } else {
    // Browser globals
    if (!root._s) {
      root._s = {};
    }
    factory(root);
  }
})(typeof window !== 'undefined' ? window : this ,function(exports, undefined){
  'use strict';

  function moduleInit($, log, conf, _, utils, IScroll, options){

    var urlConf = conf._conf;       //存储配置信息
    var _ = _||{};
    var IScroll = IScroll||{};
    var pubsub = utils.pubSub()||{};
    var callback = {};
    var callbackFuc = utils.callbackFuc;


    pubsub.addEvent('getData', function(data){
      if (data.code === 0) {
        
        utils.setHTML('.middle', data.data.username);
        pubsub.fireEvent('ordersRendered',renderOrders(data.data.products));
        pubsub.fireEvent('labelsRendered',renderLabels(data.data.tags));
      }
    });
    pubsub.addEvent('ordersRendered', function(data){
      startOrdersScroll();
    });
    pubsub.addEvent('labelsRendered', function(data){
      startLabelsScroll();
    });

    function receiver(identifier, options){
      var deferr = null;
      switch(identifier){
        case 'init':
          deferr = getOrdersTags(options);
          deferr.then(function(data){
            /*
              回调通知数据获取完毕

            */
            pubsub.fireEvent('getData',data);
          });
        break;
      }
      
      if (deferr) {
        deferr.then(function(data){
          /*
            回调通知数据获取完毕
          */
          pubsub.fireEvent(identifier,data);
        });
      }

        
    };

    /*
      M
    */
    /*
      获取订单和标签列表
    */
    function getOrdersTags(options){
      /*
        获取历史消息
      */
      var deferr = $.ajax({
        url: urlConf.apiRoot + urlConf.apiPre.one + urlConf.api.thread,
        type: 'GET',
        dataType: 'json',
        data: {
          thread_id : options.thread_id
        }
      })
      .done(function(data) {
        if(data.code === 100){
          window.location.href = urlConf.root + urlConf.static + 'login.php';
        }
          
      })
      .fail(function() {
        log._log("error");
      })
      .always(function() {
        log._log("complete");
      });
      
      return deferr;
    }

    /*
      V
    */
    /*
      渲染订单列表
    */
    function renderOrders(data){
      
      var tpl = $('#orderItemTpl');
      var container = $('#orderMain');
      var child = null;

      var width = 0;

      if (data.length>0) {
        container.html(_.template(tpl.html())({list: data}));

        child = $('#orderMain>li');

        width = child.width() + parseFloat(child.css('margin-left')) + parseFloat(child.css('margin-right'));

        container.width(width*child.length/parseFloat($('html').css('font-size')) + 'rem');

      }else{
        $('#order').remove();
      }

      return {code:0};

    }
    /*
      渲染标签列表
    */
    function renderLabels(data){
      var tpl = $('#labelTpl');
      var container = $('.jsLabelLists');
      var child = null;

      var width = 0;

      $('.jsLabelLists').html(_.template(tpl.html())({list: data}));

      child = $('.jsLabelLists>li');

      width = child.width() + parseFloat(child.css('margin-left')) + parseFloat(child.css('margin-right'));

      container.width(width*child.length/parseFloat($('html').css('font-size')) + 'rem');

    }

    /*
      C
    */

    /*
      订单列表滑动事件
    */
    function startOrdersScroll () {
      new IScroll('.order-container', {
        zoom: true,
        scrollX: true,
        scrollY: false,
        mouseWheel: true,
        tap: true,
        click: true,
        snap: true,
        snapSpeed: 400,
        keyBindings: true,
      });
    }


    /*
      标签滑动事件
    
    */
    function startLabelsScroll () {
      new IScroll('.label-container', {
        zoom: true,
        scrollX: true,
        scrollY: false,
        mouseWheel: true,
        tap: true,
        click: true,
        snap: true,
        snapSpeed: 400,
        keyBindings: true,
      });
    }




    return {
      set: function(identifier, callbacks){
        callback[identifier] = [callbacks];

        pubsub.addEvent(identifier, function(data){
          console.log(callback[identifier]);
          callbackFuc(callback[identifier],data);
        });
      },
      start: function(identifier, options){
        receiver(identifier, options);
      }
    };
  }

  exports.ordersTags = moduleInit;

});
